package com.module.lemlin.mode

import androidx.lifecycle.ViewModelProvider
import com.module.lemlin.base.BaseAbstractActivity
import com.module.lemlin.base.BaseApplication
import com.module.lemlin.base.BaseViewModel

abstract class ViewModeActivity<ViewModel : BaseViewModel> : BaseAbstractActivity() {


    lateinit var viewModel: ViewModel

    abstract fun viewModel(): ViewModel

    override fun doBeforeInit() {
        super.doBeforeInit()
        viewModel = viewModel()
        initViewMode()
    }

    /**
     * 初始化 view mode
     * */
    open fun initViewMode() = Unit

    /**
     * 生成ViewModel
     */
    inline fun <reified VM : BaseViewModel> createViewModel(): VM =
        ViewModelProvider(
            this, ViewModelProvider.AndroidViewModelFactory(BaseApplication.INSTANCE)
        )
            .get(VM::class.java)

}